package com.bkitmobile.kesu.android.GUI;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.bkitmobile.kesu.android.ClassInfo.FriendShortInfo;
import com.bkitmobile.kesu.android.connectServer.*;
public class AddFriendDialog extends Dialog {
	
	private Activity context;
	private EditText id, cmt;
	private Button btnOk, btnCancel;
	
	public AddFriendDialog(Activity context) {
		super(context);
		this.context = context;
	}
	
	protected void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
		setContentView(R.layout.friend__add_friend);
		this.id = (EditText) findViewById(R.id.friend_addFriend_id);
		this.cmt = (EditText) findViewById(R.id.friend_addFriend_cmt);
		this.btnOk = (Button) findViewById(R.id.friend_addFriend_btnOK);
		this.btnCancel = (Button) findViewById(R.id.friend_addFriend_btnCancel);
		
		this.setTitle(R.string.friend_addFriend_title);
		
		ButtonClickListener btnListener = new ButtonClickListener();
		this.btnOk.setOnClickListener(btnListener);
		this.btnCancel.setOnClickListener(btnListener);
		this.setCanceledOnTouchOutside(true);
		this.setCancelable(true);
		
	}
	
	public void show(){
		super.show();
		id.setText("");
		cmt.setText("");
	}
	
	private class ButtonClickListener implements android.view.View.OnClickListener{

		public void onClick(View v) {
			if(v == btnOk){
				//TODO: check data
				//TODO: update friends list
				String user = id.getText().toString().trim();
				if(user.equals(""))
					return;
				if(user.equals(Login.getID())){
					//TODO: xuat hien dialog bao loi
					StaticDialogs.okButtonDialog(context,
							context.getString(R.string.friend_addFriend_error_addMyself));
					return;
				}
					
				if(context instanceof FriendsList)
					
					if(((FriendsList) context).checkBeforeAddFriend(user) == false){
						StaticDialogs.okButtonDialog(context, 
								context.getString(R.string.friend_addFriend_error_alreadyInList));
				    	return;
					}
				Log.d("add", user);
			    staticClass.socket.println("<add>"+user+"<cmt>"+cmt.getText().toString().trim());		
			}
			
			AddFriendDialog.this.dismiss();
			
		}
		
	}
	
}
